//
//  LSStatusCell.swift
//  MicroBlog
//
//  Created by lj on 15/9/8.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

///控件间的间距
let LSStatusCellMargin:CGFloat = 12
/// 头像宽度
let LSStatusIconWidth:CGFloat = 35
/// 默认的图片宽度
let LSStatusPictureItemWidth:CGFloat = 90
/// 默认的图片间距
let LSStatusPictureItemMargin:CGFloat = 10
 /// 每行的最大图片数
let LSStatusPictureMaxCount:CGFloat = 3
 /// 配图视图的最大宽度
let LSStatusPictureMaxWidth = LSStatusPictureItemWidth * LSStatusPictureMaxCount + LSStatusPictureItemMargin * (LSStatusPictureMaxCount - 1)


class LSStatusCell: UITableViewCell {
    
    ///微博视图模型
    var statusViewModel: LSStatusViewModel? {
    
        didSet{
            topView.statusViewModel = statusViewModel
            
            contentLable.text = statusViewModel?.status.text
            
            pictureView.statusViewModel = statusViewModel
            
            //设置配图视图大小
            pictureViewWidthCons?.constant = pictureView.bounds.width
            pictureViewHeightCons?.constant = pictureView.bounds.height
            //根据是否有图片决定顶部约束
            pictureViewTopCons?.constant = statusViewModel?.thumbnailURLs?.count  == 0 ? 0 : LSStatusCellMargin
        }
    }
    
    /// 宽度约束
    var pictureViewWidthCons:NSLayoutConstraint?
    /// 高度约束
    var pictureViewHeightCons:NSLayoutConstraint?
    /// 顶部约束
    var pictureViewTopCons:NSLayoutConstraint?

    
    /// 计算对应模型的行高
    ///
    /// - parameter viewModel: viewModel
    ///
    /// - returns: 行高
    func rowHeight(viewModel:LSStatusViewModel) -> CGFloat {
        statusViewModel = viewModel
        
        layoutIfNeeded()
        
        return CGRectGetMaxY(bottomView.frame)
    }
    
    //MARK: -搭建界面
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        let topSeperateView = UIView()
        topSeperateView.backgroundColor = UIColor(white: 0.85, alpha: 1.0)
        
        //添加控件
        addSubview(topSeperateView)
        addSubview(topView)
        addSubview(contentLable)
        addSubview(pictureView)
        addSubview(bottomView)
        //设置布局
        let width = UIScreen.mainScreen().bounds.width
        
        topSeperateView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: self, size: CGSize(width: width, height: 10))
        topView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topSeperateView, size:  CGSize(width: width, height: 50))
        contentLable.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: LSStatusCellMargin , y: LSStatusCellMargin))
        let cons = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLable, size: CGSize(width: LSStatusPictureMaxWidth, height: LSStatusPictureMaxWidth), offset: CGPoint(x: 0, y: LSStatusCellMargin))
        //记录配图视图约束
        pictureViewWidthCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Width)
        pictureViewHeightCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Height)
        pictureViewTopCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Top)
        
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: width, height: 44), offset:CGPoint(x: -LSStatusCellMargin, y: LSStatusCellMargin))
        
//        bottomView.ff_AlignInner(type: ff_AlignType.BottomRight, referView: self, size: nil)
    }
    
    
    //MARK: -懒加载控件
    /// 顶部视图
    private lazy var topView:LSStatusCellTopView = LSStatusCellTopView()
    private lazy var contentLable:UILabel = UILabel(title: "你大爷家的二姑娘", color: UIColor.darkGrayColor(), fontSize: 15,layoutWidth:(UIScreen.mainScreen().bounds.width - 2*LSStatusCellMargin))
    private lazy var pictureView:LSStatusPictureView = LSStatusPictureView()
    private lazy var bottomView:LSStatusCellBottomView = LSStatusCellBottomView()
}
